package com.cordova.test.imageselect.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AbsListView
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.Toast
import com.bumptech.glide.Glide
import com.cordova.test.imageselect.R
import com.cordova.test.imageselect.module.ImageItem
import com.cordova.test.imageselect.view.MainActivity
import kotlinx.android.synthetic.main.image_layout.view.*
import java.util.ArrayList

class RecyclerViewAdapter(context: Context, isShowCamera: Boolean?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var context: Context? = context
    private var imageSize: Int = MainActivity.getImageItemWidth(context)
    private var imageList: ArrayList<ImageItem>? = null
    private var imageListener: OnImageItemClickListener? = null
    private var isShowCamera: Boolean = isShowCamera ?: false

    fun setImageClickListener(imageListener: OnImageItemClickListener) {
        this.imageListener = imageListener
    }

    interface OnImageItemClickListener {
        fun onItemClick(viewHolder: RecyclerView.ViewHolder, position: Int)
    }

    fun setImageList(imageList: ArrayList<ImageItem>?) {
        if (imageList == null || imageList.size == 0) {
            this.imageList = ArrayList()

        } else {
            this.imageList = imageList
        }
        notifyDataSetChanged()
    }


    fun getImageList(): ArrayList<ImageItem>? {
        return imageList
    }

    override fun onBindViewHolder(p0: RecyclerView.ViewHolder, p1: Int) {
        p0.itemView.layoutParams = AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, imageSize)
        p0.itemView.setOnClickListener { imageListener?.onItemClick(p0, p1) }
        val imageItem: ImageItem? = this.getItem(p1)
        if (p0 is CameraViewHolder) {
            bindCamera(p0, p1, imageItem)

        } else {
            bindImage(p0, p1, imageItem)
        }
    }

    private fun bindImage(viewHolder: RecyclerView.ViewHolder, position: Int, imageItem: ImageItem?) {
        Glide.with(context).load(imageItem?.path).error(R.mipmap.h).into(viewHolder.itemView.recycler_image)
        viewHolder.itemView.checkView.setOnClickListener {
            Toast.makeText(context, "click for  check  $position", Toast.LENGTH_SHORT).show()
        }
//        viewHolder.itemView.layoutParams = AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, imageSize) //让图片是个正方形
    }

    private fun bindCamera(viewHolder: RecyclerView.ViewHolder, position: Int, imageItem: ImageItem?) {
//        viewHolder.itemView.layoutParams = AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, imageSize) //让图片是个正方形
//        viewHolder.itemView.setOnClickListener { imageListener?.onItemClick(viewHolder, position) }
    }

    private fun getItem(position: Int): ImageItem? {
        return if (isShowCamera) {
            if (position == 0) null else imageList!![position - 1]
        } else {
            imageList!![position]
        }
    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
        val cameraView = LayoutInflater.from(context).inflate(R.layout.camera_layout, p0, false)
        val imageView = LayoutInflater.from(context).inflate(R.layout.image_layout, p0, false)
        return if (p1 == 0)
            CameraViewHolder(cameraView)
        else
            ImageViewHolder(imageView)
    }

    override fun getItemCount(): Int {
        if (isShowCamera) {
            return if (imageList == null) 0 else imageList!!.size + 1
        }
        return if (imageList == null) 0 else imageList!!.size
    }

    override fun getItemViewType(position: Int): Int {
        if (isShowCamera) {
            return if (position == 0) 0 else 1
        }
        return 1
    }

    inner class ImageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private var imageView: ImageView? = null
        private var checkView: FrameLayout? = null
        init {
            imageView = itemView.findViewById(R.id.recycler_image) as ImageView?
            checkView = itemView.findViewById(R.id.checkView)   as FrameLayout?
        }
    }

    inner class CameraViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

//        private var imageView: ImageView? = null

        init {
//            imageView = itemView.findViewById(R.id.recycler_image) as ImageView?
        }
    }
}